<?php

namespace App\Services;

use App\Common\Libs\ArrayHelper;
use App\Common\Libs\IdBuilder;
use App\Consts\GlobalAPIReturnConst;
use App\Consts\GlobalCookieConst;
use App\Consts\GlobalLoginConst;
use App\Consts\GlobalUserConst;
use App\Models\Cache\Cache;
use App\Models\Cache\CacheKey;
use App\Models\Field\UserField;
use App\Models\Field\UserSelect;
use App\Models\User;

/**
 * @package App\Services
 * @see InterService
 */
class InterService extends BaseService
{
    public function getAllowTouristInfo($params)
    {
        $touristId = empty($params['id']) ? IdBuilder::getUniqueID() : $params['id'];
        if (empty($params[GlobalLoginConst::LOGIN_STATE_INFO_TOKEN])) {
            $accessToken = service()->LoginService->getAccessToken(GlobalLoginConst::LOGIN_PLATFORM_WEB);
        } else {
            $accessToken = $params[GlobalLoginConst::LOGIN_STATE_INFO_TOKEN];
        }

        $loginMethod = service()->LoginService->getLoginMethodByUserIdFromCache($touristId);

        $oAuthTourist = Cache::getInstance([
            'token' => $accessToken,
        ])->hGetAll(CacheKey::OAUTH_TOURIST);
        if ($oAuthTourist) {
            return ArrayHelper::merge($oAuthTourist, [
                'identity' => GlobalUserConst::IDENTITY_TOURIST,
                'login_method' => $loginMethod,
                'server_time' => time(),
                GlobalAPIReturnConst::__COOKIES => [GlobalCookieConst::TOKEN => $accessToken],
            ]);
        }


        // 获取游客信息
        $tourist = service()->LoginService->getTourist();
        if (!empty($params['id'])) {
            $tourist['id'] = $params['id'];
        }
        // 游客id和access_token对照放入缓存，以便授权登录可以写入信息
        Cache::getInstance([
            'tourist_id' => $tourist['id']
        ])->set(
            CacheKey::TOURIST_TOKEN_SNAPSHOT,
            $accessToken,
        );

        // 游客id和access_token对照放入缓存，以便授权登录可以写入信息
        Cache::getInstance([
            'token' => $accessToken
        ])->set(
            CacheKey::TOKEN_TOURIST_SNAPSHOT,
            $tourist['id'],
        );

        return collect($tourist)->merge([
            'identity' => GlobalUserConst::IDENTITY_TOURIST,
            'login_method' => $loginMethod,
            'server_time' => time(),
            GlobalAPIReturnConst::__COOKIES => [GlobalCookieConst::TOKEN => $accessToken],
        ]);
    }

    public function getAllowUserInfo($params)
    {
        if (empty($params[GlobalLoginConst::LOGIN_STATE_INFO_TOKEN])) {
            $accessToken = service()->LoginService->getAccessToken(GlobalLoginConst::LOGIN_PLATFORM_WEB);
        } else {
            $accessToken = $params[GlobalLoginConst::LOGIN_STATE_INFO_TOKEN];
        }

        /**
         * @var User $user
         */
        $user = $params[GlobalLoginConst::LOGIN_STATE_INFO_USER];

        // 校验access_token是否在缓存中
        $verifyAccessTokenInCache = verifyAccessTokenInCache($accessToken);
        if (!$verifyAccessTokenInCache) {
            return false;
        }

        // 校验用户状态、用户是否绑定手机号
        $verifyUserStatusByUser = service()->UserService->verifyUserStatusByUser($user);
        if (is_object($verifyUserStatusByUser)) {
            return $verifyUserStatusByUser;
        }

        return collect($user->commonFormat(UserField::SELF_DETAIL))->merge([
            'identity' => GlobalUserConst::IDENTITY_USER,
            'login_method' => service()->LoginService->getLoginMethodByUserIdFromCache($user->user_id),
            'server_time' => time(),
            GlobalAPIReturnConst::__COOKIES => [GlobalCookieConst::TOKEN => $accessToken],
        ]);
    }
}
